# 准备工作
import requests
import json
import pandas as pd

# 地理编码 => 函数形式
# 函数应用方案：应用在像淘宝一样的购物app里的物流信息里，给客户提供准确的地理信息。 
def geocode(key, address, city=None, batch=None, ) -> str:
    """ 获取高德API的地理编码
       温馨提示：key是指高德api的密钥，请先去注册；
       address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo  
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address,
        "city": city,
        "batch": batch

    }
    response = requests.get(url, params=params)
    results = response.json()
    return results

# 逆地理编码
# 函数应用方案：能将后端工程师刚提取出的地理编码数据快速转换成准确的地理名称，从而快速获取某些地理位置的名称。
def regeo(key,location,extensions="base",radius=None,):
    """"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params = {'key':key,
          'location':location,
          'extensions':extensions,
             'radius':radius
             }
    r = requests.get(url,params)
    results = r.json
    return results

# 步行路径规划
# 函数应用方案：在地图app里，给客户提供多条步行导航路线。
def walking(key, origin, destination) -> str:
    """获取高德API的步行路径规划
温馨提示：key是指高德api的密钥，请先去注册；
          origin是指起始地点，destination是目的地，
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/direction
    """
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,

    }
    response = requests.get(url, params=params)
    results = response.json()
    return results

# 公交路径规划
# 函数应用方案：在地图app里，给客户提供多条公交导航路线。
def bus(key,origin,destination,city,extensions = "base",strategy = 0,nightflag = None,date = None,time = None,sig = None,output = "json",callback = None)->str:
    """获取高德API的公交路径规划
温馨提示：key是指高德api的密钥，请先去注册；
          origin是指起始地点，destination是目的地，city是城市/跨城规划时的起点城市
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/direction
    """
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params = {'key':key,
          'origin':origin,
          'destination':destination,
          'city':city,
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# 驾车路径规划
# 函数应用方案：在地图app里，给自驾者提供更贴切的驾车方案。
def car(key,origin,destination,originid = None,destinationid=None,origintype = None,destinationtype = None,strategy = 10,waypoints = None,avoidpolygons = None,avoidroad = None,province = None,number = None,cartype = 0,ferry = 0,adaggregation = 'true',nosteps = 0,sig = None,output = 'json',callback = None,extensions = 'base')->str:
    """获取高德API的驾车路径规划
温馨提示：key是指高德api的密钥，请先去注册；
          origin是指起始地点，destination是目的地
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/direction
    """
    url = 'https://restapi.amap.com/v3/direction/driving?parameters'
    params = {'key':key,
          'origin':origin,
          'destination':destination,
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# 骑行路径规划
# 函数应用方案：在地图app里，给骑行者提供多条准确的骑行路径导航方案。
def bike(key,origin,destination)->str:
    """获取高德API的骑行路径规划
温馨提示：key是指高德api的密钥，请先去注册；
          origin是指起始地点，destination是目的地
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/direction
    """
    url = 'https://restapi.amap.com/v4/direction/bicycling?parameters'
    params = {'key':key,
          'origin':origin,
          'destination':destination,
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# 货车路径规划
# 函数应用方案：在地图app里，给客户提供不同货车车辆类型提供货车导航路线方案。
def truck(key,origin,destination,size,originid = None,destinationid = None,origintype = None,destinationtype = None,strategy = 10,waypoints = None,avoidpolygons = None,avoidroad = None,province = None,number = None,cartype = 0,ferry = 0,adaggregation = 'true',nosteps = 0,sig = None,output = 'json',callback = None,extensions = 'base')->str:
    """驾车的路径规划，orgigin与destination通过上面的geo函数获得"""
    url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
    "key":key,
    "destination":destination,
    "origin":origin,
    "size":size,
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 行政区查询
# 函数应用方案：给一些需要填个人信息的网站做插件，比如当用户填入所在地是广东省时，下一级的选择可以利用行政区域规划直接返回让客户选择，而不用一条条信息输入。
def district(key,keywords,subdistrict=1,page=1,offset=20,extensions='base',filter=None,callback=None,output="JSON"):
    """行政区域规划
    keywords是你想要查询的关键词"""
    url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
    "key":key,
    "keywords":keywords,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 搜索POI
# 关键字查询
# 函数应用方案：在像美团一类的app里，给客户提供搜索服务。
def keyword(key,keywords,types,city = None,citylimit = "true",children = 1,offset = 20,page = 100,extensions = "base",sig = None,output = "json",callback = None)->str:
    """获取高德API的poi关键字搜索
温馨提示：key是指高德api的密钥，请先去注册；
          keywords是指关键字，types是关键字类型
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/search#text
    """
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params = {'key':key,
          'keywords':keywords,
          'types':types
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# 周边查询
# API功能应用方案：在像百度地图一类的app里，给客户提供搜索服务。
def near(key,location)->str:
    """获取高德API的poi周边搜索
温馨提示：key是指高德api的密钥，请先去注册；
          location是中心坐标点
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/search#around
    """
    url = 'https://restapi.amap.com/v3/place/around?parameters'
    params = {'key':key,
          'location':location
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# 多边形查询
# API功能应用方案：在像百度地图一类的app里，给客户提供搜索服务。
def polygon(key,polygon,keywords = None,types = None,offset = 25,page = 100,extensions = "base",sig = None,output = "json",callback = None)->str:
    """获取高德API的poi多边形搜索
温馨提示：key是指高德api的密钥，请先去注册；
          polygon是经纬度坐标对
          具体请查看：https://lbs.amap.com/api/webservice/guide/api/search#around
    """
    url = 'https://restapi.amap.com/v3/place/polygon?parameters'
    params = {'key':key,
          'polygon':polygon
          }
    response = requests.get(url,params = params)
    results = response.json()
    return results

# ID查询
# API功能应用方案：在像百度地图一类的app里，给客户提供搜索服务。
def searchID(key,id,callback=None,output="JSON"):
    """ID查询:ID(B0FFFAB6J2)是查询POI ID，extensions(all)为返回的数据内容，output（XML）用于指定返回数据的格式，key是用户请求数据的身份标识。"""
    url = "https://restapi.amap.com/v3/place/detail?parameters"
    params = {
    "key":key,
    "id":id,
    "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 静态地图 
# API功能应用方案：在一些网页设计页面里（比如新闻），插入相关的地理信息图片，让网页信息更加清晰可见
def staticmap(key, location, zoom, size=400 * 400, scale=1, markers=10, labels=10, paths=4, traffic=1, sig=None):
    """静态地图使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。"""
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": location,
        "zoom": zoom

    }
    r = requests.get(url, params=params)
    result = r.content
    results = Image.open(BytesIO(r.content))
    return results

# ip定位
# API功能应用方案：在手机插入相关插件，让用户快速了解自己现在所在的位置在哪里。
def IPlocation(key, ip=None, sig=None, output="json"):
    """ip定位,据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。"""
    url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key": key,
        "ip": ip

    }
    response = requests.get(url, params=params)
    results = response.json()
    # 返回值
    return results

# 坐标转换
# API功能应用方案：在高德地图里当插件，让用户在其他app里看见的地址，比如餐厅地址，能直接跳转入高德地图后直接变成高德地图的坐标，从而快速搜索到目的地结果返回给用户。
def convert(key,locations,coordsys = None,output="JSON"):
    """将能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标
    具体查看：https://lbs.amap.com/api/webservice/guide/api/convert"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
    "key":key,
    "locations":locations,
     "coordsys":coordsys,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 天气查询
# API功能应用方案：放到旅游定制方案的app里，给用户提供某个目标区域的天气查询，从而给客户提供更加有依据的旅游方案定制。
def weather(key,city,extensions = "base",output = "json"):
    """根据用户输入的adcode，查询目标区域当前/未来的天气情况。
    具体查看：https://lbs.amap.com/api/webservice/guide/api/weatherinfo"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
    "key":key,
    "city":city
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 输入提示
# API功能应用方案：应用在地图类的app里，在用户输入时为他们提Fong返回建议的列表，从而减轻用户的记忆负担和使用繁琐度，从而创造一个良好的用户体验。
def input(key,keywords,location = None,city = None,citylimit = "true",datatype = "all",sig = None,output = "json",callback = None):
    """提供根据用户输入的关键词查询返回建议列表。
    具体查看：https://lbs.amap.com/api/webservice/guide/api/inputtips"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
    "key":key,
    "keywords":keywords
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 交通姿态
# API功能应用方案：应用在地图类的app里，在平时的时候，可以让用户快速了解路况从而选择最适合的路线，而对与正在行驶的司机则可以提供大致停滞时间还有多久，给客户及时的反馈。
def traffic(key,extensions = "base",sig = None,output = "json",callback = None,rectangle = None):
    """提供根据用户输入的内容能够返回希望查询的交通态势情况
    具体查看：https://lbs.amap.com/api/webservice/guide/api/trafficstatus"""
    url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params = {
    "key":key,
    "rectangle":rectangle,
    "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results